package com.itstyle.jpa.repository;

import com.itstyle.jpa.model.Organization;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OrganizationRepository extends JpaRepository<Organization, String>, JpaSpecificationExecutor {



//    Organization findByOrgCode(String orgCode);
//
//    @Query(value = "select org from Organization  org " +
//            "join Employee emp on org.orgCode = emp.orgCode " +
//            "where emp.empId=:empId")
//    Organization findByEmpId(String empId);
//
//    //@Query(value="select  org  from Organization  org where org.isDeleted=:isDeleted")
//    List<Organization> findByIsDeletedAndTypeIn(Integer isDeleted, List<Integer> typeList);
//    List<Organization> findByIsDeletedAndTypeInAndBrandCodeIn(Integer isDeleted, List<Integer> typeList,List<String> brandCode);
//    List<Organization> findByIsDeletedAndTypeAndLevelAfter(Integer isDeleted, Integer type,Integer level);
//    List<Organization> findByIsDeleted(Integer isDeleted);
//    List<Organization> findByLevelBetweenOrderByLevel(Integer start,Integer end);
//    List<Organization> findByLevelBetweenOrderByLevelDesc(Integer start,Integer end);
//
//    @Query(value="select id from Organization where orgCodeSidm=:orgCode")
//    List<Long> queryIdByCode(String orgCode);
//    @Modifying
//    @Transactional
//    @Query(value="update organization org,organization org1 set org.parent_org_id=org1.id where org.parent_org_code = org1.org_code",nativeQuery = true)
//    void updateParentId();
//
//    @Query(value = "select  org_code_sidm as org_code,org_name from organization  where level =3 and type=1 group by org_code_sidm ,org_name",nativeQuery = true)
//    List<Object[]> queryMonetaryArea();
//
//    @Query(value = "select org_code_sidm,org_name from organization where parent_org_code_sidm =:orgCode and level =5  and  type=1 group by org_code_sidm,org_name",nativeQuery = true)
//    List<Object[]> queryRegion(String orgCode);
//
//    @Query(value = "select org_code_sidm,org_name from organization where parent_org_code_sidm =:orgCode and level =5  and  type=1 group by org_code_sidm,org_name",nativeQuery = true)
//    List<Object[]> queryShop(String orgCode);
//
//    @Query(value = "select org_code_sidm,org_name from organization where parent_org_code_sidm =:orgCode and level =5  and  type=1 group by org_code_sidm,org_name" ,nativeQuery = true)
//    List<Object[]> queryBusinessManager(String orgCode);
    Organization findByIdAndIsDeleted(Long id, Integer isDeleted);

    @Query(value = "SELECT count(1) from (\n" +
            "SELECT id FROM `organization` where parent_org_id in (SELECT id FROM `organization` where parent_org_id in (SELECT id FROM `organization` where parent_org_id =3) ) ) a WHERE id=:orgCode " ,nativeQuery = true)
    Integer queryOrganizationCodeIsNull(@Param("orgCode") Integer orgCode);


    List<Organization> findByParentOrgId(long poId);
}
